package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.BusAcquisitionCardChannel;
import com.ruoyi.business.mapper.BusAcquisitionCardChannelMapper;
import com.ruoyi.business.service.IBusAcquisitionCardChannelService;
import com.ruoyi.business.service.IBusHardwareStatusCommonUpdateService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusAcquisitionCardMapper;
import com.ruoyi.business.domain.BusAcquisitionCard;
import com.ruoyi.business.service.IBusAcquisitionCardService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 数据采集卡Service业务层处理
 * 
 * @author asher
 * @date 2021-10-17
 */
@Service
public class BusAcquisitionCardServiceImpl implements IBusAcquisitionCardService, IBusHardwareStatusCommonUpdateService
{
    @Autowired
    private BusAcquisitionCardMapper busAcquisitionCardMapper;

    @Autowired
    private IBusAcquisitionCardChannelService busAcquisitionCardChannelService;

    private static final String INIT_CHANNEL_NAME = "通道";

    private static final String INIT_CHANNEL_REMARK = "";

    private static final String INIT_CHANNEL_CODE = "ai";

    /**
     * 查询数据采集卡
     * 
     * @param acquisitionCardId 数据采集卡主键
     * @return 数据采集卡
     */
    @Override
    public BusAcquisitionCard selectBusAcquisitionCardByAcquisitionCardId(Long acquisitionCardId)
    {
        return busAcquisitionCardMapper.selectBusAcquisitionCardByAcquisitionCardId(acquisitionCardId);
    }

    /**
     * 查询数据采集卡列表
     * 
     * @param busAcquisitionCard 数据采集卡
     * @return 数据采集卡
     */
    @Override
    public List<BusAcquisitionCard> selectBusAcquisitionCardList(BusAcquisitionCard busAcquisitionCard)
    {
        return busAcquisitionCardMapper.selectBusAcquisitionCardList(busAcquisitionCard);
    }

    /**
     * 新增数据采集卡
     * 
     * @param busAcquisitionCard 数据采集卡
     * @return 结果
     */
    @Override
    public int insertBusAcquisitionCard(BusAcquisitionCard busAcquisitionCard)
    {
        busAcquisitionCard.setCreateTime(DateUtils.getNowDate());
        return busAcquisitionCardMapper.insertBusAcquisitionCard(busAcquisitionCard);
    }

    /**
     * 修改数据采集卡
     * 
     * @param busAcquisitionCard 数据采集卡
     * @return 结果
     */
    @Override
    public int updateBusAcquisitionCard(BusAcquisitionCard busAcquisitionCard)
    {
        busAcquisitionCard.setUpdateTime(DateUtils.getNowDate());
        BusAcquisitionCard busAcquisitionCard1 = busAcquisitionCardMapper.selectBusAcquisitionCardByAcquisitionCardId(busAcquisitionCard.getAcquisitionCardId());
        if (!busAcquisitionCard1.getChannelAmount().equals(busAcquisitionCard.getChannelAmount())) {
            boolean b = handleChannelsInit(busAcquisitionCard);
        }
        int i = busAcquisitionCardMapper.updateBusAcquisitionCard(busAcquisitionCard);
        return i;
    }

    /**
     * 批量删除数据采集卡
     * 
     * @param acquisitionCardIds 需要删除的数据采集卡主键
     * @return 结果
     */
    @Override
    public int deleteBusAcquisitionCardByAcquisitionCardIds(Long[] acquisitionCardIds)
    {
        return busAcquisitionCardMapper.deleteBusAcquisitionCardByAcquisitionCardIds(acquisitionCardIds);
    }

    /**
     * 删除数据采集卡信息
     * 
     * @param acquisitionCardId 数据采集卡主键
     * @return 结果
     */
    @Override
    public int deleteBusAcquisitionCardByAcquisitionCardId(Long acquisitionCardId)
    {
        return busAcquisitionCardMapper.deleteBusAcquisitionCardByAcquisitionCardId(acquisitionCardId);
    }

    @Override
    public int updateStatusByDestRecordId(Long destRecordId, int destStatus, Date updateTime) {
        return busAcquisitionCardMapper.updateStatusByDestRecordId(destRecordId, destStatus, updateTime);
    }

    @Transactional
    @Override
    public int insertBusAcquisitionCardWithChannels(BusAcquisitionCard busAcquisitionCard) {
        int i = busAcquisitionCardMapper.insertBusAcquisitionCard(busAcquisitionCard);
        return handleChannelsInit(busAcquisitionCard) ? i : 0;
    }

    private boolean handleChannelsInit(BusAcquisitionCard busAcquisitionCard) {
        busAcquisitionCardChannelService.deleteBusAcquisitionCardChannelByCardId(busAcquisitionCard.getAcquisitionCardId());
        String channelAmount = busAcquisitionCard.getChannelAmount();
        BusAcquisitionCardChannel busAcquisitionCardChannel = new BusAcquisitionCardChannel();
        busAcquisitionCardChannel.setAcquisitionCardId(busAcquisitionCard.getAcquisitionCardId());

        Date nowDate = DateUtils.getNowDate();
        busAcquisitionCardChannel.setCreateTime(nowDate);
        busAcquisitionCardChannel.setUpdateTime(nowDate);
        busAcquisitionCardChannel.setCreateBy(busAcquisitionCard.getCreateBy());
        busAcquisitionCardChannel.setUpdateBy(busAcquisitionCard.getUpdateBy());
        busAcquisitionCardChannel.setName(INIT_CHANNEL_NAME);
        busAcquisitionCardChannel.setRemark(INIT_CHANNEL_REMARK);
        busAcquisitionCardChannel.setCode(INIT_CHANNEL_CODE);

        ArrayList<BusAcquisitionCardChannel> busAcquisitionCardChannels = new ArrayList<>();
        Integer channelSize = Integer.valueOf(channelAmount);
        for (int i = 0; i < channelSize; i++) {
            busAcquisitionCardChannels.add(i, busAcquisitionCardChannel);
        }
        int resultRows = busAcquisitionCardChannelService.insertBusAcquisitionCardChannelBatch(busAcquisitionCardChannels);
        return resultRows == channelSize;
    }
}
